﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;

namespace PickGold.SqlServer
{
	/// <summary>
	/// Triggers
	/// </summary>
	public static partial class Triggers
	{
		//public static Dictionary<string, string[]> Tables;
		/// <summary>
		/// Trigger
		/// </summary>
		[SqlTrigger]//(Name = "Trigger", Target = "Table", Event = "FOR UPDATE")
		public static void Trigger()
		{
			var t = "=" + SqlContext.TriggerContext.ColumnCount;
			for (var i = SqlContext.TriggerContext.ColumnCount - 1; i >= 0; i--)
				t = (SqlContext.TriggerContext.IsUpdatedColumn(i) ? '1' : '0') + t;
			t = ":" + t;
			if (SqlContext.TriggerContext.EventData != null && !SqlContext.TriggerContext.EventData.IsNull)
				t = ":" + SqlContext.TriggerContext.EventData.Value + t;
			t = "Demo(Trigger Fired):" + SqlContext.TriggerContext.TriggerAction + t;
			SqlContext.Pipe.Send(t);// 用您的代码替换

			//using (var con = Common.OpenConnection(null))
			//{
			//    using (var cmd = con.CreateCommand())
			//    {
			//        switch (SqlContext.TriggerContext.TriggerAction)
			//        {
			//            case TriggerAction.Insert:
			//                cmd.CommandText = "SELECT [item_id], [item_uuid], [item_flag], [item_version], 'inserted' [item_table], @return [item_error] FROM [inserted] AS [tb_tst_demo_info] FOR XML AUTO, TYPE, BINARY BASE64";
			//                break;
			//            case TriggerAction.Update:
			//                break;
			//            case TriggerAction.Delete:
			//                break;
			//        }
			//        var t = cmd.ExecuteScalar() as string;
			//        SqlContext.Pipe.Send(t);
			//    }
			//    Common.CloseConnection(con, null);
			//}
		}
	}
}
